/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-03-03
*/
package com.rzico.basics.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Brand;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Tag;
import com.rzico.basics.service.BrandService;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.TagService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 搜索接口
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-03-03
 */

@Api(description = "搜索接口")
@RestController
@RequestMapping("/seq")
public class SeqController extends BaseController {

    @Autowired
    private TagService tagService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private BrandService brandService;


    /**
     * 热门搜索
     *
     * @return
     */
    @ApiOperation("热门搜索")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "mchId", value = "商户id", dataType = "String", paramType = "query")
    })
    public CommResult<Brand> list(String mchId, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sortField", "orders");
        params.put("sortType","ASC");

        if (StringUtils.isNotEmpty(mchId)){
            params.put("mchId", mchId);
        }

        Enterprise enterprise = enterpriseService.findByMch(mchId);
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }
        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Brand> list = brandService.selectList(params);

        List<String> data = new ArrayList<>();
        for (Brand brand:list) {
            data.add(brand.getName());
        }
        return CommResult.success(data);
    }


}
